angular.module('Controller')
.controller('QgyPayEarnestModalCtrl',($scope,urls,$localStorage,qgyApiService,NgTableParams,commonUtil,cryptUtil,wfAlert,commonConstant,$http, $uibModalInstance, items)->
    $scope.type=type=items.type
    $scope.data=data=items.data
    $scope.modalTitle='交定金'
    $scope.offPayTypeList=commonConstant.offPayTypeList()
    $scope.earnest={
        phone:undefined
        money:undefined
        planTime:undefined
        payType:3
        name:undefined
#        payMoney:undefined
        moneyDesc:undefined
    }
    $scope.planTimeList=[]
    loadDateList=()->
        list=[]
        for i in [0..9]
            today=moment()
            dateStr=today.add(i, 'day').format('YYYY-MM-DD')
            list.push({value:dateStr,name:dateStr})
        $scope.planTimeList=list
        $scope.earnest.planTime=moment().add(1,'day').format('YYYY-MM-DD')
    loadDateList()
    $scope.confirmWord=()->
        tmp=_.find($scope.offPayTypeList,(item)->item.value==$scope.earnest.payType)
        str='确认用户通过'+tmp?.name+'支付了定金'+String($scope.earnest.money)+'元？'
        console.log(str)
        return str
    $scope.fetchUser=()->
        if $scope.earnest.phone?.length==11
            pm=qgyApiService.fetchAsyncUser($scope.earnest.phone)
            pm.then((data)->
                $scope.earnest.name=data.customerName
            )
    $scope.confirm = ()->
        reqData={
            houseId:data.id
            customerPhone:$scope.earnest.phone,
            customerName:$scope.earnest.name
            money:Number($scope.earnest.money),
            payMoney:Number($scope.earnest.money),
            payType:$scope.earnest.payType,
            planCheckIn:$scope.earnest.planTime,
            moneyDesc:$scope.earnest.moneyDesc
        }
        $http.post("/earnest",reqData)
        .success((resp)->
            if resp.code==0
                wfAlert.toastInfo($scope.modalTitle+'成功')
                $uibModalInstance.close('done')
            else
                wfAlert.toastError(resp.message)
        )
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')
)